<template>
  <div class="login-container">
    <el-form :model="ruleForm2" :rules="rules2"
             status-icon
             ref="ruleForm2"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm login-page">
      <h3 class="title">注册</h3>
      <el-form-item prop="phone">
        <el-input type="text"
                  v-model="ruleForm2.phone"
                  auto-complete="off"
                  placeholder="手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-input type="text"
                  v-model="ruleForm2.email"
                  auto-complete="off"
                  placeholder="邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="ruleForm2.password"
                  auto-complete="off"
                  placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="repeat">
        <el-input type="repeat"
                  v-model="ruleForm2.repeat"
                  auto-complete="off"
                  placeholder="重复密码"
        ></el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { register } from '@/api/user'
    export default {
      name: "register",
      data(){
        const validatePass2 = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请再次输入密码'))
          } else if (value !== this.ruleForm2.password) {
            callback(new Error('两次输入密码不一致!'))
          } else {
            callback()
          }
        };
        return {
          logining: false,
          redirect: undefined,
          ruleForm2: {
            phone: '',
            email : '',
            password: '',
            repeat:'',
          },
          rules2: {
            phone: [{required: true, message: '请输入手机号', trigger: 'blur'}],
            email: [{required: true, message: '请输入邮箱', trigger: 'blur'}],
            password: [{required: true, message: '请输入密码', trigger: 'blur'}],
            repeat: [{required: true, validator: validatePass2, trigger: 'blur'}]
          },
          checked: false
        }
      },
      methods: {
        handleSubmit(event){
          // 校验重复密码是否重复
          this.$refs.ruleForm2.validate((valid) => {
            if (valid) {
              register(this.ruleForm2).then(() => {
                this.$router.push('/app/index/login');
              }).catch((response) => {
                console.log(response);
              })
            } else {
              console.log('error submit!!')
              return false
            }
          })
        }
      },
    }
</script>

<style scoped>
  .login-container {
    width: 100%;
    padding-top: 35%;
  }
  .login-page {
    width: 100%;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    text-align: center;
  }
  label.el-checkbox.rememberme {
    margin: 0px 0px 15px;
    text-align: left;
  }
</style>
